﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using FastReport;
using FastReport.Utils;
using System.Windows.Forms;

namespace YinLong.ReportEx
{

    #region 使用说明

    /*
    打印
    WYL.Framework.ReportEX.WYLReport report = new WYL.Framework.ReportEX.WYLReport();
    FastReport.Report r = (FastReport.Report)report.GetReport(dst, "WYL长期医嘱单2");
    r.Report.PrintSettings.ShowDialog = false;
    r.Report.Print();
    预览
    WYL.Framework.ReportEX.WYLReport report = new WYL.Framework.ReportEX.WYLReport();
    report.ReportPreview(dst, "WYL长期医嘱单1");
    设计
    WYL.Framework.ReportEX.WYLReport report = new WYL.Framework.ReportEX.WYLReport();
    report.ReportDesigner(dst, "WYL长期医嘱单1");
     数据库脚本

    CREATE TABLE[dbo].[Base_Report]
    (

    [ReportId][int] IDENTITY(1,1) NOT NULL,

    [ReportName] [nvarchar] (50) NULL,
    [Remark] [nvarchar] (500) NULL,
    [ReportContent] [ntext] NULL,
    [ReportHeight] [numeric] (18, 2) NULL,
    [ReportWeith] [numeric] (18, 2) NULL
    ) ON[PRIMARY] TEXTIMAGE_ON[PRIMARY]

     */

    #endregion

    public class WYLReport
    {
        public WYLReport()
        {
            this.SetLanguage();
        }
        public void SetLanguage()
        {
            try
            {
                Res.LocaleFolder = "Localization";
                Res.LoadLocale(Res.LocaleFolder + "\\Chinese (Simplified).frl");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool ReportDesigner(System.Data.DataSet ds, string reportName)
        {
            return this.ReportDesigner(this.GetReportForData(ds), reportName);
        }
        public bool ReportDesigner(Report fReport, string reportName)
        {
            string text = Path.GetDirectoryName(Application.ExecutablePath) + "\\Report";
            text = text + "\\" + reportName + ".frx";
            //ReportInfo reportInfo = new ReportInfo();
            //reportInfo = this.GetTheReport(reportName);
            //if (reportInfo.repContent.Length > 0)
            //{
            //    this.CreateReporFile(text, reportInfo.repContent);
            //}
            fReport.FileName = text;
            if (File.Exists(fReport.FileName))
            {
                fReport.Load(fReport.FileName);
            }
            new FrmReportDesigner(fReport)
            {
                StartPosition = FormStartPosition.CenterScreen,
                WindowState = FormWindowState.Maximized
            }.ShowDialog();
            bool result;
            if (!File.Exists(fReport.FileName))
            {
                result = false;
            }
            return true;
            //else
            //{
            //    QuerySolutionProxy querySolutionProxy = new QuerySolutionProxy();
            //    ReportInfo reportInfo2 = new ReportInfo();
            //    reportInfo2 = this.GetTheReport(reportName);
            //    string text2 = fReport.SaveToString();
            //    string commandText;
            //    if (reportInfo2.repContent.Length > 0)
            //    {
            //        commandText = string.Concat(new string[]
            //        {
            //            "update Base_Report set reportcontent = '",
            //            text2,
            //            "' where reportname = '",
            //            reportName,
            //            "'"
            //        });
            //    }
            //    else
            //    {
            //        commandText = string.Concat(new string[]
            //        {
            //            "insert into Base_Report (reportname,remark,reportcontent) values ('",
            //            reportName,
            //            "','",
            //            reportName,
            //            "','",
            //            text2,
            //            "')"
            //        });
            //    }
            //    querySolutionProxy.ExecCustomUpdate(commandText);
            //    result = true;
            //}
            //return result;
        }
        public bool ReportPreview(System.Data.DataSet data, string report)
        {
            return this.ReportPreview(this.GetReportForData(data), report);
        }
        private bool ReportPreview(Report reportDes, string reportname)
        {
            bool result;
            if (reportDes == null)
            {
                result = false;
            }
            else
            {
                string text = Path.GetDirectoryName(Application.ExecutablePath) + "\\Report";
                text = text + "\\" + reportname + ".frx";
                try
                {
                    //ReportInfo reportInfo = new ReportInfo();
                    //reportInfo = this.GetTheReport(reportname);
                    //if (reportInfo.repContent.Length > 0)
                    //{
                    //    this.CreateReporFile(text, reportInfo.repContent);
                    //}
                    if (!File.Exists(text))
                    {
                        if (MessageBox.Show("此报表系统中暂时没有提供，是否现在进行设计 ？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            result = this.ReportDesigner(reportDes, reportname);
                        }
                        else
                        {
                            result = false;
                        }
                    }
                    else
                    {
                        reportDes.Load(text);
                        FrmReportPreView frmReportPreView = new FrmReportPreView(reportDes, reportname);
                        frmReportPreView.WindowState = FormWindowState.Maximized;
                        frmReportPreView.StartPosition = FormStartPosition.CenterScreen;
                        frmReportPreView.ShowDialog();
                        reportDes.Dispose();
                        frmReportPreView.Dispose();
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return result;
        }
        public bool ReportPreview(DataGridView dgv, string dbName, string report)
        {
            bool result;
            if (dgv.Columns.Count <= 0)
            {
                result = false;
            }
            else
            {
                System.Data.DataTable dataTable = new System.Data.DataTable(dbName);
                for (int i = 0; i < dgv.Columns.Count; i++)
                {
                    Type type = dgv.Columns[i].ValueType;
                    if (type == null)
                    {
                        type = typeof(string);
                    }
                    System.Data.DataColumn dataColumn = new System.Data.DataColumn(dgv.Columns[i].HeaderText, type);
                    dataColumn.ColumnName = dgv.Columns[i].HeaderText;
                    dataColumn.Caption = dgv.Columns[i].Name;
                    dataTable.Columns.Add(dataColumn);
                }
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    System.Data.DataRow dataRow = dataTable.NewRow();
                    for (int j = 0; j < dgv.Columns.Count; j++)
                    {
                        dataRow[dgv.Columns[j].HeaderText] = dgv.Rows[i].Cells[j].Value;
                    }
                    dataTable.Rows.Add(dataRow);
                }
                result = this.ReportPreview(this.GetReportForData(new System.Data.DataSet
                {
                    Tables =
                    {
                        dataTable
                    }
                }), report);
            }
            return result;
        }
        public object GetReport(System.Data.DataSet dtSet, string reportName)
        {
            Report reportForData = this.GetReportForData(dtSet);
            return this.GetReport(reportForData, reportName);
        }
        private object GetReport(Report reportDes, string reportName)
        {
            string text = AppDomain.CurrentDomain.BaseDirectory + "Report";
            if (!Directory.Exists(text))
            {
                Directory.CreateDirectory(text);
            }
            text = text + "\\" + reportName + ".frx";
            object result;
            try
            {
                //ReportInfo reportInfo = new ReportInfo();
                //reportInfo = this.GetTheReport(reportName);
                //if (reportInfo.repContent.Length > 0)
                //{
                //    this.CreateReporFile(text, reportInfo.repContent);
                //}
                if (!File.Exists(text))
                {
                    if (MessageBox.Show("此报表系统中暂时没有提供，是否现在进行设计 ？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        if (this.ReportDesigner(reportDes, reportName))
                        {
                            result = reportDes;
                        }
                        else
                        {
                            result = null;
                        }
                    }
                    else
                    {
                        result = null;
                    }
                }
                else
                {
                    reportDes.Load(text);
                    result = reportDes;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        //private ReportInfo GetTheReport(string reportname)
        //{
        //    ReportInfo reportInfo = new ReportInfo();
        //    string sql = "Select * from Base_Report where reportname = '" + reportname + "'";
        //    QuerySolutionProxy querySolutionProxy = new QuerySolutionProxy();
        //    System.Data.DataTable dataTable = querySolutionProxy.ExeAMentod(sql).Tables[0];
        //    if (dataTable.Rows.Count > 0)
        //    {
        //        reportInfo.repid = Convert.ToInt32(dataTable.Rows[0]["ReportId"].ToString());
        //        reportInfo.repName = dataTable.Rows[0]["ReportName"].ToString();
        //        reportInfo.report = dataTable.Rows[0]["Remark"].ToString();
        //        reportInfo.repContent = dataTable.Rows[0]["ReportContent"].ToString().Replace("?<?xml version=", "<?xml version=");
        //    }
        //    else
        //    {
        //        reportInfo.repContent = "";
        //    }
        //    return reportInfo;
        //}
        private Report GetReportForData(System.Data.DataSet ds)
        {
            Report report = new Report();
            report.RegisterData(ds);
            return report;
        }
        private void CreateReporFile(string fileName, string strContent)
        {
            StreamWriter streamWriter = new StreamWriter(fileName);
            streamWriter.Write(strContent);
            streamWriter.Flush();
            streamWriter.Dispose();
        }
        public object GetReport(System.Data.DataSet dtSet, string reportName, object reportObj)
        {
            Report report = (Report)reportObj;
            if (report == null)
            {
                report = new Report();
            }
            object result;
            if (report.IsRunning || report.IsPrinting || report.IsDesigning)
            {
                result = report;
            }
            else
            {
                report.RegisterData(dtSet);
                result = this.GetReport(report, reportName);
            }
            return result;
        }
    }
    //public class ReportInfo
    //{
    //    public int repid
    //    {
    //        get;
    //        set;
    //    }
    //    public string repName
    //    {
    //        get;
    //        set;
    //    }
    //    public string report
    //    {
    //        get;
    //        set;
    //    }
    //    public string repContent
    //    {
    //        get;
    //        set;
    //    }
    //    public double paperWidth
    //    {
    //        get;
    //        set;
    //    }
    //    public double paperHeight
    //    {
    //        get;
    //        set;
    //    }
    //    public int orgid
    //    {
    //        get;
    //        set;
    //    }
    //}
}
